$(function(){
    obj.init();
});

window.obj = new function() {
    'use strict';
    return {
        dataGrid: null,
        params: {
            schoolId: null,
            userId: null
        },
        init: function () {
            this.params.schoolId = xhTool.getQuery('schoolId');
            this.params.userId   = xhTool.getQuery('userId');

            this.dataGrid = new xhDataGrid($('#list'), 'Appgroup/getAppGroup', {schoolId: this.params.schoolId, userId: this.params.userId});
            this.dataGrid.parseToolbar().init().activeBtn($('#bt_list_clean'), $('#bt_list_edit, #bt_list_del, #bt_list_user, #bt_list_app, #bt_list_copy'));
            xhExtra.clearLoadSuccess(this.dataGrid);

            this.listener();
        },
        listener: function () {
            var $this = this;
            xhTool.bindDocKeyDown($('#bt_list_search').click(function () {
                $this.funObj().search();
            }));
            //新建组
            $('#bt_list_add').click(function(){
                $this.funObj().addGroup();
            });
            //删除组
            xhExtra.bindListDel($this.dataGrid, 'groupId', 'Appgroup/delAppGroup');
            //关联用户
            $('#bt_list_user').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().concatUser();
            });
            //关联应用
            $('#bt_list_app').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().concatApp();
            });
            //修改
            $('#bt_list_edit').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().editGroup();
            });
            //复制
            $('#bt_list_copy').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().copyGroup();
            });
            //一键清理
            $('#bt_list_clean').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().autoClean();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                search: function () {
                    $this.dataGrid.reload({
                        name: xhExtra.getValue($('#list_vague'))
                    });
                },
                addGroup: function() {
                    window.top.swal({
                        title: '新建应用组',
                        // input: 'text',
                        // inputPlaceholder: '组名称',
                        html: '<input id="group_name" class="swal2-input" placeholder="组名称" />' +
                        '<select id="group_type" class="swal2-select">' +
                        '<option value="2">教师组</option>' +
                        '<option value="1">学生组</option>' +
                        '<option value="4">通用组</option>' +
                        '</select>',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        showLoaderOnConfirm: true,
                        preConfirm: function () {
                            return new Promise(function (resolve, reject) {
                                var groupName = $('#group_name', window.top.document).val(),
                                    groupType = $('#group_type', window.top.document).val();
                                if (!groupName) {
                                    reject('组名称非法！');
                                } else {
                                    xhExtra.ajaxRequest('Appgroup/createAppGroup',
                                        {groupName: groupName, schoolId: $this.params.schoolId, groupType: groupType},
                                        {success: true, table: $this.dataGrid});
                                }
                            })
                        },
                        allowOutsideClick: false
                    }).then(function (groupName) {
                    }, function(dismiss) {});
                },
                concatUser: function() {
                    window.top.xhTop.showModal({
                        title: $this.dataGrid.getSelectValue('groupName') + '-用户',
                        content: xhTool.getFrameString('Appgroup/appGroupUser', {groupId: $this.dataGrid.getSelectValue('groupId'), schoolId: $this.params.schoolId}, 500),
                        size: 'lg',
                        button: '保存',
                        backClose: false
                    });
                },
                concatApp: function() {
                    window.top.xhTop.showModal({
                        title: $this.dataGrid.getSelectValue('groupName') + '-应用',
                        content: xhTool.getFrameString('Appgroup/appGroupApp', {groupId: $this.dataGrid.getSelectValue('groupId'), schoolId: $this.params.schoolId}, 500),
                        size: 'lg',
                        button: true,
                        backClose: false
                    });
                },
                editGroup: function() {
                    window.top.swal({
                        title: '修改应用组',
                        // input: 'text',
                        // inputPlaceholder: '组名称',
                        html: '<input id="group_name" class="swal2-input" placeholder="组名称" />' +
                        '<select id="group_type" class="swal2-select">' +
                        '<option value="2">教师组</option>' +
                        '<option value="1">学生组</option>' +
                        '<option value="4">通用组</option>' +
                        '</select>',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '修改',
                        showLoaderOnConfirm: true,
                        preConfirm: function () {
                            return new Promise(function (resolve, reject) {
                                var groupName = $('#group_name', window.top.document).val(),
                                    groupType = $('#group_type', window.top.document).val();
                                if (!groupName) {
                                    reject('组名称非法！');
                                } else {
                                    xhExtra.ajaxRequest('Appgroup/modifyAppGroup',
                                        {groupId: $this.dataGrid.getSelectValue('groupId'), groupName: groupName, groupType: groupType},
                                        {success: true, table: $this.dataGrid});
                                }
                            })
                        },
                        allowOutsideClick: false,
                        onOpen: function() {
                            $('#group_name', window.top.document).val($this.dataGrid.getSelectValue('groupName'));
                            $('#group_type', window.top.document).find("option:contains('" + $this.dataGrid.getSelectValue('userType') + "')").attr('selected', true);
                        }
                    }).then(function (groupName) {
                    }, function(dismiss) {});
                },
                copyGroup: function () {
                    xhExtra.askChangeStatus('Appgroup/copyGroup', {groupId: $this.dataGrid.getSelectValue('groupId')}, $this.dataGrid, '确认复制应用组？');
                },
                autoClean: function () {
                    xhExtra.askChangeStatus('Appgroup/autoCleanUser', {schoolId: $this.params.schoolId, groupIds: $this.dataGrid.getSelectValues('groupId')}, $this.dataGrid, '清理选中组历史账号？');
                }
            }
        }
    }
}